﻿var global_host_id = 1;

var Host = function(ip,port){
	var that = this;
	window.WebSocket = window.WebSocket || window.MozWebSocket;
	var isWebSocket = !(!window.WebSocket);
	//var isWebSocket = false; 

		
	if(ip == undefined)
		//ip = "127.0.0.1";
		ip = "202.59.152.227";
	if(port == undefined)
		port = isWebSocket ? "14503" : "12012";
	
	var last_connect_date = new Date();
	
	var can_send = false;
	var is_connecting = true;
	var id = global_host_id + "";
	var first_com = true;
	global_host_id++;
	
	
	that.send = function(msg){
		if (isWebSocket) {
			if(can_send)
			{
				try {
			        if (socket != null) {
			            socket.send(msg);
			        }
			    } catch (exception) {
			        alert(exception);       
			    }
			}
		}
		else {
			if (!can_send && !first_com) {
				setTimeout("send('" + msg + "')", 100);
				return;
			}
			uexSocketMgr.sendData(id, msg + "\r\n");
		}
	};
		
	that.command = function(com, data){
		if (data == undefined) 
			that.send(com);
		else 
			that.send(com + " " + data);
	};
		
	that.disconnect = function(){
		if(isWebSocket)
		{
			try {
		        if(socket != null)
		        {
		            socket.close();
		        }
		    } catch (exception) {
		        alert(exception);       
		    }
		}
		else
			uexSocketMgr.closeSocket(id);
	};
	
	var socket;
	
	if (isWebSocket) {
		var host = "ws://" + ip + ":" + port + "/";
		socket = new WebSocket(host);

        socket.onopen = function () {
    		can_send = true;
    	};

        socket.onmessage = function (msg) {
		    var list = msg.data.split(" ");
		    var com = list[0];
		    var data = msg.data.substring(com.length).trim();
		    try {
		        var json_data = eval("(" + data + ")");
		        eval(com + "_Command(json_data)");
		    }
		    catch (exception) {
		        eval(com + "_Command(data)");
		    }
		};

        //socket.onclose = onclose;
        //socket.onerror = onerror;
	}
	else {
		uexSocketMgr.createTCPSocket(id);
		
		uexSocketMgr.setInetAddressAndPort(id, ip, port);
		
		uexSocketMgr.cbSendData = function(opId, dataType, data){
			if (first_com) {
				if (data == 0) {
					can_send = true;
				}
				else {
					alert("连接服务器失败!");
				}
			}
			else {
				if (data == 1) {
					alert("发送指令失败!");
				}
			}
		};
		
		uexSocketMgr.onData = function(opId, msg){
			var command_list = msg.split("\r\n");
			for (var i = 0; i < command_list.length; i++) {
				var temp = command_list[i];
				if (temp == "") 
					break;
				if (temp.indexOf("{") == 0) 
					temp = temp.substring(temp.indexOf("}") + 1);
				
				var list = temp.split(" ");
				var com = list[0];
				var data = temp.substring(com.length).trim();
				
				try {
					var json_data = eval("(" + data + ")");
					eval(com + "_Command(json_data,that)");
				} 
				catch (exception) {
					eval(com + "_Command(data,that)");
				}
			}
		};
						
		that.command("Test");
	}
};

Host.prototype = {};
window.Host = Host;

String.prototype.trim = function () {
    var str = this.replace(/^(\s|\u00A0)+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

